package com.mcf.xiaolinju.service.impl;

import com.mcf.xiaolinju.entity.UserDetails;
import com.mcf.xiaolinju.mapper.UserDetailsMapper;
import com.mcf.xiaolinju.vo.LoginReturn;
import com.mcf.xiaolinju.entity.User;
import com.mcf.xiaolinju.mapper.CodeMapper;
import com.mcf.xiaolinju.mapper.UserMapper;
import com.mcf.xiaolinju.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mcf.xiaolinju.token.JwtUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author mcf
 * @since 2021-10-11
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Resource
    UserMapper userMapper;
    @Resource
    CodeMapper codeMapper;
    @Resource
    UserDetailsMapper userDetailsMapper;
    //登录
    public LoginReturn selectByUsernamePassword(String username, String password,String code){
        LoginReturn loginReturn=new LoginReturn();
//        String  encryption = CommonFuction.getMD5String(password);
           User user=userMapper.selectByUsernamePassword(username,password);
           //得到生成的验证码
           String getcode=codeMapper.selectByMaxId();
           //判断验证码
           if(getcode.equals(code)) {



               if (user != null) {
                   if(user.getState()==1) {
                       //判断角色
                       if (user.getRole() != 2) {
                           loginReturn.setToken(JwtUtil.getTokn(user.getId(), user.getMastername()));
                           loginReturn.setMessage("登陆成功");
                           loginReturn.setId(user.getId());
                           loginReturn.setMastername(user.getMastername());
                           loginReturn.setRole(user.getRole());

                           return loginReturn;
                       } else {
                           loginReturn.setMessage("你的身份不是管理员不能登录");
                           return loginReturn;
                       }
                   }
                   else if(user.getState()==0){
                       loginReturn.setMessage("账户不存在");
                       return loginReturn;
                   }
                   else{
                       loginReturn.setMessage("账户管理员审批还未通过");
                       return loginReturn;
                   }
               } else {

                   loginReturn.setMessage("账户或密码错误");

                   return loginReturn;
               }


           }
           else{
               loginReturn.setMessage("验证码不正确");
               return loginReturn;

           }


    }
    //获得验证旧密码
    public  String getPassword(int id, String pass){
       // String  newpass = CommonFuction.getMD5String(pass);
         String password=userMapper.getPassword(id);
         //if(password.equals(newpass))
        if(password.equals(pass)){
            return "right";
        }
        else{
            return "您输入的旧密码不正确";
        }
    }
    //通过id修改密码
    public String updatePassword(int id,String password,int updateid){
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
        //String newpassword=CommonFuction.getMD5String(password);
        if(userMapper.updatePassword(id,password,updateid,ldt)>0)
        //if(userMapper.updatePassword(id,newpassword)>0)
        {
            return "修改成功";
        }
        return  "修改失败";
    }
    //通过用户名查询用户是否存在
    public int getUserCount(String username){
        return  userMapper.getUserCount(username);
    }
    //通过用户名修改密码
    public String updatePasswordByUsername(String username,String password){
        Date date=new Date();
        LocalDateTime ldt=date.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime();
         if(userMapper.updatePasswordByUsername(username,password,ldt)>0)
         {
             return "修改成功";
         }
        return  "修改失败";
    }
   //小程序登录
    public LoginReturn applogin(String username, String password) {
        LoginReturn loginReturn=new LoginReturn();
//        String  encryption = CommonFuction.getMD5String(password);
        User user=userMapper.selectByUsernamePassword(username,password);
            if (user != null) {
                if(user.getState()==1) {
                    loginReturn.setToken(JwtUtil.getTokn(user.getId(), user.getMastername()));
                    loginReturn.setMessage("登陆成功");
                    loginReturn.setId(user.getId());
                    loginReturn.setMastername(user.getMastername());
                    loginReturn.setRole(user.getRole());
                    return loginReturn;
                }
                else if(user.getState()==0){
                    loginReturn.setMessage("账户不存在");
                    return loginReturn;
                }
                else if(user.getState()==3){
                    loginReturn.setMessage("账户管理员拒绝了你的申请");
                    return loginReturn;
                }
                else{
                    loginReturn.setMessage("账户管理员审批还未通过");
                    return loginReturn;
                }
                }
                else {
                loginReturn.setMessage("账户或密码错误");

                return loginReturn;
            }
    }

    public LoginReturn getUserAPI(String username) {
        LoginReturn loginReturn=new LoginReturn();
        User user=userMapper.selectByUsername(username);
        if(user !=null){
            loginReturn.setId(user.getId());
            return loginReturn;
        }
        else {
            loginReturn.setMessage("该用户不存在");
            return loginReturn;
        }

    }
    @Transactional(rollbackFor =  Exception.class)
    public String insertusermessage(String username, String password, int role, String mastername, String idcardnumber, int communityid, String unitnum, int buildingnum, int housenum, String phonenum) {

        if (userMapper.insertuser(username,password,role,mastername)>0) {
            Integer num = userMapper.getId(username);
            System.out.println("num");
            System.out.println(num);
            if (num != null) {
                if (userDetailsMapper.insertusermessage(num,idcardnumber,communityid,unitnum,buildingnum,housenum,phonenum)>0) {
                    return "成功请等待审批";
                } else {
                    TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                    return "申请失败";
                }
            } else {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "申请失败";
            }
        } else {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return "申请失败";
        }
    }
}
